package com.starblink.basic.style.view.empty

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.core.view.updateLayoutParams
import com.blankj.utilcode.util.SizeUtils
import com.starblink.basic.style.R
import com.starblink.basic.style.databinding.WidgetEmptyViewBinding
import com.starblink.basic.style.view.border.internal.dp

/**
 * 空View
 */
class SkEmptyView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleRes: Int = 0
) : LinearLayout(context, attrs, defStyleRes) {

    private val binding: WidgetEmptyViewBinding =
        WidgetEmptyViewBinding.inflate(LayoutInflater.from(context), this)

    var titleText: CharSequence? = null
        set(value) {
            field = value
            binding.tvTitle.text = value
        }

    var titleTextColor: Int = ContextCompat.getColor(context, R.color.sk_gray800)
        set(value) {
            field = value
            binding.tvTitle.setTextColor(value)
        }

    var titleTextSize: Float = 18f
        set(value) {
            field = value
            binding.tvTitle.textSize = value
        }

    var titleMaxLines: Int = 1
        set(value) {
            field = value
            binding.tvTitle.maxLines = value
        }

    var hideTitle: Boolean = true
        set(value) {
            field = value
            binding.tvTitle.visibility = if (field) GONE else VISIBLE
        }


    var text1: CharSequence? = null
        get() =
            binding.tvText1.text
        set(value) {
            field = value
            binding.tvText1.text = value
        }

    var text1Color: Int = ContextCompat.getColor(context, R.color.gu_text_primary)
        set(value) {
            field = value
            binding.tvText1.setTextColor(value)
        }

    var text1Size: Float = 16f
        set(value) {
            field = value
            binding.tvText1.textSize = value
        }

    var text1MaxLines: Int = 1
        set(value) {
            field = value
            binding.tvText1.maxLines = value
        }

    var hideText1: Boolean = false
        set(value) {
            field = value
            binding.tvText1.visibility = if (field) GONE else VISIBLE
        }


    var text2: CharSequence? = null
        get() =
            binding.tvText2.text
        set(value) {
            field = value
            binding.tvText2.text = value
        }

    var text2Color: Int = ContextCompat.getColor(context, R.color.gu_text_secondary)
        set(value) {
            field = value
            binding.tvText2.setTextColor(value)
        }

    var text2Size: Float = 14f
        set(value) {
            field = value
            binding.tvText2.textSize = value
        }

    var text2MaxLines: Int = 3
        set(value) {
            field = value
            binding.tvText2.maxLines = value
        }

    var hideText2: Boolean = false
        set(value) {
            field = value
            binding.tvText2.visibility = if (field) GONE else VISIBLE
        }


    var btnText: CharSequence? = null
        set(value) {
            field = value
            binding.button.text = value
        }

//    var contentPaddingTop: Int = 50
//        set(value) {
//            field = value
//            binding.content.setPadding(
//                30.dp,
//                value.dp,
//                30.dp,
//                0
//            )
//        }

    var btnBackground: Int? = null
        set(value) {
            field = value
            value?.let { binding.button.setBackgroundResource(it) }
        }

    var btnShown: Boolean = false
        set(value) {
            field = value
            binding.button.visibility = if (value) VISIBLE else GONE
        }

    var isFullScreenWidth: Boolean = false
        set(value) {
            field = value
            binding.img.updateLayoutParams<ViewGroup.LayoutParams> {
                width = 191.dp
                height = 142.dp
            }
        }

    var onBtnClick: (() -> Unit)? = null
        set(value) {
            field = value
            binding.button.setOnClickListener {
                value?.invoke()
            }
        }

    init {
        orientation = VERTICAL

        var typeArray: TypedArray? = null
        try {
            typeArray = context.obtainStyledAttributes(attrs, R.styleable.SkEmptyView)
            titleText = typeArray.getString(R.styleable.SkEmptyView_empty_titleText)
            titleTextColor = typeArray.getColor(
                R.styleable.SkEmptyView_empty_titleTextColor,
                ContextCompat.getColor(context, R.color.gu_text_primary)
            )
            titleTextSize = typeArray.getDimension(R.styleable.SkEmptyView_empty_titleTextSize, 32f)
            titleMaxLines = typeArray.getInt(R.styleable.SkEmptyView_empty_titleMaxLines, 1)
            hideTitle = typeArray.getBoolean(R.styleable.SkEmptyView_empty_hideTitle, true)

            text1 = typeArray.getString(R.styleable.SkEmptyView_empty_text1)
            if (text1.isNullOrEmpty()) {
                text1 = resources.getString(R.string.default_empty_text1)
            }
            text1Color = typeArray.getColor(
                R.styleable.SkEmptyView_empty_text1Color,
                ContextCompat.getColor(context, R.color.gu_text_primary)
            )
            text1Size = typeArray.getDimension(R.styleable.SkEmptyView_empty_text1Size, 16f)
            text1MaxLines = typeArray.getInt(R.styleable.SkEmptyView_empty_text1MaxLines, 1)
            hideText1 = typeArray.getBoolean(R.styleable.SkEmptyView_empty_hideText1, false)

            text2 = typeArray.getString(R.styleable.SkEmptyView_empty_text2)
            text2Color = typeArray.getColor(
                R.styleable.SkEmptyView_empty_text2Color,
                ContextCompat.getColor(context, R.color.gu_text_secondary)
            )
            text2Size = typeArray.getDimension(R.styleable.SkEmptyView_empty_text2Size, 14f)
//            contentPaddingTop = typeArray.getDimension(R.styleable.SkEmptyView_empty_paddingTop, 16f)
            text2MaxLines = typeArray.getInt(R.styleable.SkEmptyView_empty_text2MaxLines, 3)
            hideText2 = typeArray.getBoolean(R.styleable.SkEmptyView_empty_hideText1, false)

            btnText = typeArray.getString(R.styleable.SkEmptyView_empty_btnText)
            btnBackground = typeArray.getResourceId(
                R.styleable.SkEmptyView_empty_btnTextBackground,
                R.drawable.sp_empty_btn
            )
            btnShown = typeArray.getBoolean(R.styleable.SkEmptyView_empty_btnShown, false)

            isFullScreenWidth = typeArray.getBoolean(R.styleable.SkEmptyView_empty_fullScreenWidth, false)

            val paddingTop = typeArray.getDimensionPixelSize(R.styleable.SkEmptyView_empty_paddingTop, 50)
            val paddingBottom = typeArray.getDimensionPixelSize(R.styleable.SkEmptyView_empty_paddingBottom, 30)
            setPadding(SizeUtils.dp2px(24f), paddingTop, SizeUtils.dp2px(24f), paddingBottom)
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            typeArray?.recycle()
        }
    }
}